﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;

namespace Fonlow.BetterRobocopyGui
{
    [Flags]
    [TypeConverter(typeof(CopyFlagsConverter))]
    public enum CopyFlags : int
    {
        None = 0, D = 1, A = 2, T = 4, S = 8, O = 16, U = 32
    }

    /// <summary>
    /// R – Read only A – Archive S – System
    /// H – Hidden C – Compressed N – Not content indexed
    /// E – Encrypted T – Temporary O - Offline
    /// </summary>
    [Flags]
    [TypeConverter(typeof(RashcnetoFlagsConverter))]
    public enum Rashcneto : int
    {
        None = 0, R = 1, A = 2, S = 4, H = 8, C = 16, N = 32, E = 64, T = 128, O = 256
    }

    public class CopyFlagsConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        const int flagTotal = 6;
        const string flagCharacters = "DATSOU";
        const string emptyString = "None";

        /// <summary>
        /// convert to string representation
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                int flagsInt = (int)value;
                string str = String.Empty;
                for (int i = 0; i < flagTotal; i++)
                {
                    int flagMark = 1 << i;
                    if ((flagMark & flagsInt) != 0)
                    {
                        str += ((CopyFlags)flagMark).ToString();
                    }
                }

                if (str == string.Empty)
                    return emptyString;
                else
                    return str;

            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string str = value as string;
            if (str != null)
            {
                char[] v = str.ToCharArray();
                int flagsInt = 0;
                foreach (char s in v)
                {
                    int index = flagCharacters.IndexOf(Char.ToUpper(s));
                    if (index >= 0)
                    {
                        flagsInt |= 1 << index;
                    }
                    else // so invalid character in the string
                    {
                        return null;
                    }
                }
                return (CopyFlags)flagsInt;
            }

            return base.ConvertFrom(context, culture, value);
        }
    }

    public class RashcnetoFlagsConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        const int flagTotal = 9;
        const string flagCharacters = "RASHCNETO";
        const string emptyString = "None";

        /// <summary>
        /// convert to string representation
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                int flagsInt = (int)value;
                string str = String.Empty;
                for (int i = 0; i < flagTotal; i++)
                {
                    int flagMark = 1 << i;
                    if ((flagMark & flagsInt) != 0)
                    {
                        str += ((Rashcneto)flagMark).ToString();
                    }
                }

                if (str == string.Empty)
                    return emptyString;
                else
                    return str;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string str = value as string;
            if (str != null)
            {
                char[] v = str.ToCharArray();
                int flagsInt = 0;
                foreach (char s in v)
                {
                    int index = flagCharacters.IndexOf(Char.ToUpper(s));
                    if (index >= 0)
                    {
                        flagsInt |= 1 << index;
                    }
                    else // so invalid character in the string
                    {
                        return null;
                    }
                }
                return (Rashcneto)flagsInt;
            }

            return base.ConvertFrom(context, culture, value);
        }
    }

    public class StringEventArgs : EventArgs
    {
        public StringEventArgs(string message)
        {
            Message = message;
        }

        public string Message { private set; get; }
    }


}
